﻿#region Using

using System;
using System.Collections.Generic;
using System.Linq;

#endregion

namespace Common.Base
{
    public class RemovableQueue<T>
    {
        private readonly LinkedList<T> list = new LinkedList<T>();

        public int Count
        {
            get
            {
                return this.list.Count;
            }
        }

        public void Enqueue(T t)
        {
            this.list.AddLast(t);
        }

        public T Dequeue()
        {
            var result = this.list.First.Value;
            this.list.RemoveFirst();
            return result;
        }

        public T Peek()
        {
            return this.list.First.Value;
        }

        public bool Remove(T t)
        {
            return this.list.Remove(t);
        }

        public void RemoveAll(Predicate<T> predicate)
        {
            for (int i = 0; i < this.list.Count; i++)
            {
                var item = this.Dequeue();
                if (!predicate(item))
                {
                    this.Enqueue(item);
                }
            }
        }

        public IList<T> ToList()
        {
            return this.list.ToList();
        }

        public bool Contains(T t)
        {
            return this.list.Contains(t);
        }

        public void Clear()
        {
            this.list.Clear();
        }

        public IEnumerable<T> Take(int count)
        {
            for (var i = 0; i < count; i++)
            {
                yield return this.Dequeue();
            }
        }
    }
}